<?
/*
	function getTestMail()
	{
		$mail= new MailMime();
		$mail->setHeader('demonit@ria.ua','Test message');
		$mail->setTXTBody($this->getTemplate('element_mail_txt.tpl'));
		$mail->setHTMLBody($this->getTemplate('element_mail_html.tpl'));
		$mail->addAttachment('index.php','text/plain');
		
		
		$mail->save('demonit@ria.ua','temp.txt');
		
		if ($mail->send('demonit@ria.ua'))
		{
			print 'send';
		}
		
	}
*/

include('Mail.php');
include('Mail/mime.php');

class MailMime{
	
	private $mime;
	private $header=array(
                            'From'=>'no-reply@ria.ua',
                            'Subject'=>'',
                            'charset'=>'Windows-1251');
	
	/*Creates a new instance of Mail_Mime*/
	function __construct($build_params = array(
                             'text_encoding' => '8bit',
                             'html_encoding' => 'quoted-printable',
                             '7bit_wrap'     => 998,
                             'html_charset'  => 'Windows-1251',
                             'text_charset'  => 'Windows-1251',
                             'head_charset'  => 'Windows-1251'
                            )) {
		$this->mime = new Mail_mime();
		$this->mime->_build_params = $build_params;
	}

	/**
	 * @param string $from
	 * @param string $subject
	 * @param array $array
	 */
	function setHeader($from,$subject,$header=array())
	{	
		
		$this->header = array_merge($this->header,array('From'=>$from,'Subject'=>$subject));

		if (count($header))
		{
			$this->header = array_merge($this->header,$header);
		}
	}
	
	/*Sets the plain text part of a message*/
	
	function setTXTBody($text)
	{
		return $this->mime->setTXTBody($text);
	}
	
	/*Sets the HTML part of a message*/
	
	function setHTMLBody($html)
	{
		return $this->mime->setHTMLBody($html);
	}
	
	/* Adds an attachment to a message.
	 * string $file - The file name or the data itself
	 * string $c_type - The content type of the image or file. (text/html, text/plain, image/jpeg, image/gif, application/pdf, ...)
	*/
	
	function addAttachment($file,$file_type='',$file_name='')
	{
		return $this->mime->addAttachment($file,$file_type,$file_name);
	}
	
	/*sending of a MIME mail*/
	
	function send($email)
	{
		$body = $this->mime->get();
		$hdrs = $this->mime->headers($this->header);
		$mail = & Mail::factory('mail');
		return $mail->send($email, $hdrs, $body);
	}
	
	function sendmail_via_file($to) {
		$file_name='/tmp/_sendmail_'.time().'.txt';
		$this->save($to,$file_name);
		return $this->sendmail($file_name);
	}
	
	function sendmail($file){
		# sendmail($email,$project_name='')
		# $project
		#$file = '/tmp/'.$project_name.time();
		#$this->save($email,$file);
		system('/usr/sbin/sendmail -t -i < '.$file);
		unlink($file);
		return true;
	}
	
	function save($email,$file)
	{
		$body = $this->mime->get();
		$hdrs = $this->mime->headers($this->header);
		
		$mail =& Mail::factory('mail');
		$mail->sep="\r\n";
		$headerElements=$mail->prepareHeaders($hdrs);
		list($from, $text_headers) = $headerElements;
		
		if ($fp=fopen($file,'w'))
		{
			fwrite($fp,'To: '.$email."\r\n");
			fwrite($fp,$text_headers."\r\n\r\n");
			fwrite($fp,$body);
			fclose($fp);
			return true;
		}
		return false;
	}
}
?>